package org.example.test_qlexpress.function;

import com.ql.util.express.Operator;

import java.math.BigDecimal;
import java.math.BigInteger;

public class SumFunction extends Operator {
    public Object executeInner(Object[] objects) throws Exception {
        BigDecimal sum = BigDecimal.ZERO;
        if (objects == null || objects.length == 0) {
            return sum;
        }
        for (Object object : objects) {
            if (object == null) {
                continue;
            }
            BigDecimal value;
            if (object instanceof Byte) {
                value = BigDecimal.valueOf(((Byte) object).longValue());
            } else if (object instanceof Short) {
                value = BigDecimal.valueOf(((Short) object).longValue());
            } else if (object instanceof Integer) {
                value = BigDecimal.valueOf(((Integer) object).longValue());
            } else if (object instanceof Long) {
                value = BigDecimal.valueOf((Long) object);
            } else if (object instanceof Float) {
                value = BigDecimal.valueOf(((Float) object).doubleValue());
            } else if (object instanceof Double) {
                value = BigDecimal.valueOf((Double) object);
            } else if (object instanceof BigInteger) {
                value = BigDecimal.valueOf(((BigInteger) object).longValue());
            } else if (object instanceof BigDecimal) {
                value = (BigDecimal) object;
            } else {
                throw new RuntimeException("不支持该类型的参数：" + object);
            }
            sum = sum.add(value);
        }
        return sum;
    }
}
